import Vue from 'vue'
import Router from 'vue-router'
import routerJs from "./router.js"
import auth from "../service/auth.js"
Vue.use(Router)
const vueRouter = new Router({
  routes: routerJs
})
vueRouter.beforeEach((to, from, next) => {
  //如果没登录，则只允许跳转到login、register、retrievepass【找回密码】
  var RouterName = to.name;
  if (RouterName !== 'login') {
    if (!auth.loggedIn()) {
      // alert('请先登录后操作');
      next({
        path: '/login',
      });
      return;
    }
  }

  //如果登录了则不允许跳转到login和register、retrievepass【找回密码】
  if (RouterName === 'login') {
    if (auth.loggedIn()) {
      next({
        path: '/'
      });
      return;
    }
  }

  next();
});
export default vueRouter;
